package com.it.hanzhongtravelv1.service;

import com.it.hanzhongtravelv1.dto.AccommodationDTO;
import com.it.hanzhongtravelv1.entity.Accommodation;
import com.it.hanzhongtravelv1.repository.AccommodationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AccommodationService {

    @Autowired
    private AccommodationRepository accommodationRepository;

    public List<AccommodationDTO> getAllAccommodations() {
        return accommodationRepository.findAll()
                .stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }

    public AccommodationDTO getAccommodationById(Long id) {
        return accommodationRepository.findById(id)
                .map(this::convertToDto)
                .orElse(null);
    }

    public AccommodationDTO saveAccommodation(Accommodation accommodation) {
        Accommodation saved = accommodationRepository.save(accommodation);
        return convertToDto(saved);
    }

    public void deleteAccommodation(Long id) {
        accommodationRepository.deleteById(id);
    }

    public List<AccommodationDTO> getAccommodationsByType(String type) {
        return accommodationRepository.findByType(type)
                .stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }

    public List<AccommodationDTO> getAccommodationsByLocation(String location) {
        return accommodationRepository.findByLocationContaining(location)
                .stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }

    private AccommodationDTO convertToDto(Accommodation accommodation) {
        AccommodationDTO dto = new AccommodationDTO();
        dto.setId(accommodation.getId());
        dto.setName(accommodation.getName());
        dto.setDescription(accommodation.getDescription());
        dto.setImage(accommodation.getImage());
        dto.setLocation(accommodation.getLocation());
        dto.setType(accommodation.getType());
        dto.setPriceRange(accommodation.getPriceRange());
        dto.setFacilities(accommodation.getFacilities());
        dto.setNearbyAttractions(accommodation.getNearbyAttractions());
        return dto;
    }
} 